const qiniu = require("../../utils/qiniu");
Page({
    onLoad: function (options) {
        wx.showNavigationBarLoading()
        getApp().http.get("city/member/member-info").then(res => {
            wx.hideNavigationBarLoading()
            if (res.data.province) {
                res.data.area = [res.data.province, res.data.city]
            } else {
                res.data.area = []
            }
            this.setData({
                info: res.data
            })
            return getApp().http.get("city/member/qn-token")
        }).then(res => {
            this.setData({
                token: res.data
            })
        })
    },
    setSex(evt) {
        const list = ['男', '女', '保密']
        wx.showActionSheet({
            itemList: list,
            success: res => {
                this.setData({
                    "info.sex": list[res.tapIndex]
                })
            }
        })
    },
    setBirthday(evt) {
        this.setData({
            "info.birthday": evt.detail.value
        })
    },
    setAvatar(evt) {
        wx.chooseImage({
            count: 1,
            success: res => {
                wx.showNavigationBarLoading()
                qiniu.upload(res.tempFilePaths, this.data.token).then(keys => {
                    this.img = keys[0]
                    wx.hideNavigationBarLoading()
                })
                this.setData({
                    "info.img": res.tempFilePaths[0]
                })
            }
        })
    },
    setArea(evt) {
        evt.detail.value.length = 2
        this.setData({
            "info.area": evt.detail.value
        })
    },
    setIsOpen(evt) {
        console.log(evt.detail)
        this.setData({
            "info.is_open": evt.detail.value ? 1 : 0
        })
    },
    save() {
        let post = this.data.info
        if (post.area.length) {
            post.province = post.area[0]
            post.city = post.area[1]
        }
        post.img = this.img || post.img
        delete post.area

        if (this.data.loading) {
            return
        }
        this.setData({
            loading: true
        })
        getApp().http.post("city/member/update-member-info", { data: post }).then(res => {
            this.setData({
                loading: false
            })
            wx.navigateBack()
        })
    },
    showName() {
        this.setData({
            isName: 1
        })
    },
    closeMask() {
        this.setData({
            isName: false
        })
    },
    onInput(evt) {
        this[evt.target.dataset.key] = evt.detail.value
    },
    submitInput(evt) {
        const { key } = evt.target.dataset
        if (!this[key]) {
            getApp().wxToast.warn("请输入")
        }
        this.setData({
            ["info." + key]: this[key]
        })
        this.closeMask()
    }
})